Module# 02: Generic Programming
Lecture#02: Generic methods
// Example 2.1: A generic method
for printing
class DemoClass {
//
Defining a generic method to print any data type
void genericPrint
(T t) {
System.out.println
(t);
}
public static void main(String[] args)
{
DemoClass aObj;
// Creating an object of the
class DemoClass
aObj.genericPrint(101); // Calling generic method with int argument
aObj.genericPrint("Joy with Java"); // Calling generic method with String
aObj.genericPrint(3.1412343); // Calling generic method with double
}
}
// Example 2.2: Static generic
method
class StaticGenericMethodDemo{
//
Defining a static generic method to print any data type
static <T> void genericPrint
(T t){
//The
following statement print which type parameter T this method is handling
System.out.println
(t.getClass().getName() + ":" + t);
}
public static void main(String[] args){
genericPrint(101); // Calling generic method with integer argument
genericPrint("Joy with Java"); // Calling generic method with String argument
genericPrint(3.1412343); // Calling generic method with double argument
}
}
//Example 2.3: Generic method for Integer
swap operation
class SwapTest1{
public static void swap(T x, T y){
T temp;
t = x;
x = y;
y = t;
}
public static void main(String args[]){
Integer x = new Integer(99);
Integer y = new Integer(66);
System.out.println("x = +
x + " + "y = + y);
swap(x, y);
System.out.println("x = +
x + " + "y = + y);
}
}
// Example 2.4: Generic method for
Double swap operation
class SwapTest2{
public static void swap(T x, T y){
T temp;
t = x;
x = y;
y = t;
}
public static void main(String args[]){
Double x = new Double(99.0);
Double y = new Double(66.0);
System.out.println("x = +
x + " + "y = + y);
swap(x, y);
System.out.println("x = +
x + " + "y = + y);
}
}
//Example 2.5: Generic method for String
swap operation
class SwapTest3{
public static void swap(T x, T y){
T temp;
t = x;
x = y;
y = t;
}
public static void main(String args[]){
String x = "99";
String y = "66";
System.out.println("x = +
x + " + "y = + y);
swap(x, y);
System.out.println("x = +
x +" + "y = + y);
}
}
//Example 2.6: Swap method with
Object as parameters
class Person {
String name;
float marks;
Person(String
name, float marks) {
this.name = name; this.marks = marks
}
}
class SwapTest4{
public static void swap(Object x, Object y){
Object t;
t = x;
x = y;
y = t;
}
public static void main(String args[]){
Object p1 = new Person(Sumit, 99.9);
Double p2 = new Double(Rahul, 66.6);
System.out.println(p1 = + p1 + " + "y = + p2);
swap(p1, p2);
System.out.println(p1 = + p1 + " + "y = + p2);
}
}
// Example 2.7: varargs method using array
class VarargsMethodDemo1 {
static void varargsMethod1(int v[])
{
System.out.print("Number of args: " + v.length
+" Elements: ");
for(int x : v)
System.out.print(x + " ");
System.out.println();
}
public static void main(String args[]) {
//
Following arrays are created for test...
int x[] = { 1, 3, 5, 7 };
int y[] = { 2, 4};
int z[] = { };
varargsMethod1 (x); // Passed 4 values to the method
varargsMethod1 (y); // Passed 2 values to the method
varargsMethod1 (z); // Passed no argument to the method
}
}
// Example 2.8: varargs method using Ellipsis
class VarargsMethodDemo2 {
//Defining
a varargs method using ellipsis
static void varargsMethod2(int ...v) {
System.out.println("Number of arguments: " + v.length);
for (int i: v) // For each item i in
array v
System.out.print(i + " ");
System.out.println();
}
public static void main(String args[]) {
// Calling the varargs
method with variable arguments
varargsMethod2 (9);
// One parameter
varargsMethod2 (1, -2, 3, -4); // Four parameters
varargsMethod2 ();
// no parameter
}
}
//Example 2.9: varargs method using Object
class VarargsMethodDemo3 {
public static void
varargsMethod3(Object ... obj) {
for(Object o : obj)
System.out.print( + o);
System.out.println( );
}
public static void main(String[] args) {
varargsMethod3(
1, String, 2.3, true); //
Four arguments
varargsMethod3 (); // No arguments
varargsMethod3 (15, 25, 35, 45, 55);
//
Five arguments
}
}